<?php
// 1908080121李定邦、1902030115李龙烽
declare (strict_types = 1);
namespace app\admin\controller;
use app\BaseController;
use think\App;

class Index extends BaseController {

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->checkIsAdminLogin();
    }

    public function index() {
        $admin_name = app('db')->name('admin_user')->where('id', session('login_admin_id'))->value('username');
        $domain     = array_keys(config('app.domain_bind'));
        $domain     = array_shift($domain);
        return app('view')->fetch('', ['admin_name' => $admin_name, 'front_domain' => $domain]);
    }

    /**
     * 帖子管理
     * @return mixed
     */
    public function forum() {
        $secList = $this->listSec();
        $sid     = $this->request->get('sid');
        $keyword = $this->request->get('keyword');
        $mes     = app('db')->name('mes')->where("m_title", "LIKE", "%$keyword%");
        if (!empty($sid)) {
            $mes = $mes->where('m_sid', $sid);
        }
        $mes->where('is_del', 0);
        $mes  = $mes->order('m_createat', 'desc')->paginate(10);
        $page = $mes->render();
        $data = [
            'sec_list' => $secList,
            'list'     => $mes,
            'page'     => $page,
        ];

        return app('view')->fetch('', $data);
    }

    /**
     * 帖子详情页
     * @param int $mid
     * @return mixed
     */
    public function forumDetail(int $mid = 0) {
        $info = app('db')->name('mes')->where('m_id', $mid)->where('is_del', 0)->find();

        return app('view')->fetch('', ['info' => $info]);
    }

    /**
     * 删除帖子
     * @return \think\response\Json
     */
    public function forumDelete() {
        $m_ids = $this->request->get('m_id');
        $m_ids = is_array($m_ids) ? $m_ids : [$m_ids];
        $res   = app('db')->name('mes')->whereIn('m_id', $m_ids)->update(['is_del' => 1]);
        $data = [
          'rs' => true,
          'msg' => $res ? '删除成功' : '未知错误'
        ];

        return json($data);
    }

    /**
     * 帖子留言
     * @param int $mid
     * @return mixed
     */
    public function reslist($mid = 0)
    {
        $res = app('db')->name('res')->where("r_mid", $mid);
        $res->where('is_del', 0);
        $res  = $res->order('r_createat', 'desc')->paginate(10);
        $page = $res->render();
        $info = app('db')->name('mes')->where('m_id', $mid)->find();
        $data = [
            'list'  => $res,
            'page'  => $page,
            'info'  => $info,
        ];

        return app('view')->fetch('', $data);
    }

    /**
     * 帖子回复删除
     * @return \think\response\Json
     */
    public function resDelete() {
        $r_ids = $this->request->get('r_id');
        $r_ids = is_array($r_ids) ? $r_ids : [$r_ids];
        $res   = app('db')->name('res')->whereIn('r_id', $r_ids)->update(['is_del' => 1]);
        $data = [
            'rs' => true,
            'msg' => $res ? '删除成功' : '未知错误'
        ];

        return json($data);
    }
}
